package com.ymatou.iapi.optrade.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFeeTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFreightTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.req.ValidateRefundReq;
import com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.BargainGroupService;
import com.ymatou.iapi.optrade.service.RefundService;
import com.ymatou.iapi.optrade.service.ValidateRefundCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceAuthenticateCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.business.service.YMTTrading.TradingProductVo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 退货退款申请效验接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ValidateRefund {
	private static ValidateRefundReq validateRefundReq;
	private static ValidateRefundCall applyvalidationCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long userId = 20238699;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("退货退款申请校验接口-客服用");
	}

	@Before
	public void caseUp() {
		validateRefundReq = new ValidateRefundReq();
		applyvalidationCall = new ValidateRefundCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(applyvalidationCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_001() {
		Logger.start(true, "用户下单后申请效验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithCoupon();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);

			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals(ECode.FAILED201,
					applyvalidationCall.getString("code"), "验证返回码");

			Logger.verifyEquals("114", applyvalidationCall.getString("bcode"),
					"验证返回码");

			Logger.verifyEquals("申请退款金额不能大于实际可退款金额",
					applyvalidationCall.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_002() {
		Logger.start(true, "用户支付后接单前效验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals("0", applyvalidationCall.getString("bcode"),
					"验证返回码");
			Logger.verifyEquals(false, applyvalidationCall.getData()
					.getBoolean("isValidationPass"), "验证isValidationPass");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_003() {
		Logger.start(true, "商家接单前取消订单,效验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.cancelOrderBeforePaid(CancelOrderTypeEnum.BUYER_CANCEL_ORDER);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals(
					"订单已取消，不能退货退款：" + validateRefundReq.getOrderId(),
					applyvalidationCall.getString("msg"), "验证返回码");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_004() {
		Logger.start(true, "商家接单后,退货退款校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals(true,
					applyvalidationCall.getData()
							.getBoolean("isValidationPass"),
					"验证isValidationPass");

			Logger.verifyEquals(0,
					(Integer) applyvalidationCall.getData()
							.get("refundFreight"), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_005() {
		Logger.start(true, "商家接单后取消订单,效验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();

			// 取消订单

			TradingCallService.cancelOrder(orderId,
					CancelOrderTypeEnum.CUSTOMER_CANCEL_ORDER, userId);

			Logger.comment("前置数据准备结束");
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());
			validateRefundReq.setRefundNumber(1);

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals(false, applyvalidationCall.getData()
					.getBoolean("isValidationPass"), "验证isValidationPass");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ValidateRefund_006() {
		Logger.start(false, "订单退货退款中,申请校验的金额小于等于可退款金额");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 付款			
			TradingCallService.payOrder(userId, amount, tradingId);

			// 卖家接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals("0", applyvalidationCall.getString("bcode"),
					"验证返回码");
			Logger.verifyEquals(true,
					applyvalidationCall.getData()
							.getBoolean("isValidationPass"),
					"验证isValidationPass");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ValidateRefund_007() {
		Logger.start(false, "订单退货退款中,申请校验的金额大于最大可退款金额");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderApplyRefund();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals("申请退款金额不能大于实际可退款金额",
					applyvalidationCall.getString("msg"), "验证msg");
			Logger.verifyEquals(false, applyvalidationCall.getData()
					.getBoolean("isValidationPass"), "申请退款金额不能大于实际可退款金额");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_008() {
		Logger.start(true, "用户收货之后申请校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderConfirmReceive();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals(true,
					applyvalidationCall.getData()
							.getBoolean("isValidationPass"),
					"验证isValidationPass");
			Logger.verifyEquals(0,
					(Integer) applyvalidationCall.getData()
							.get("refundFreight"), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ValidateRefund_009() {
		Logger.start(false, "订单号不存在申请效验");
		try {

			validateRefundReq.setOrderId(11111);
			validateRefundReq.setCatalogId("123");
			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);
			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals(ECode.FAILED201,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals("订单不存在:" + validateRefundReq.getOrderId(),
					applyvalidationCall.getString("msg"), "验证错误提示");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ValidateRefund_010() {
		Logger.start(false, "订单号为null申请效验");
		try {
			// validateRefundReq.setOrderId(111111);
			validateRefundReq.setCatalogId("123");
			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);
			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals(ECode.FAILED400,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals("orderId:订单号不能为空",
					applyvalidationCall.getString("msg"), "验证错误提示");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_014() {
		Logger.start(true, "商家发货后,退货退款校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderDelivery();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals(true,
					applyvalidationCall.getData()
							.getBoolean("isValidationPass"),
					"验证isValidationPass");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_015() {
		Logger.start(true, "确认收货15天后,退货退款校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderConfirmReceive();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			OrderPo record = new OrderPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DAY_OF_MONTH, -16);;

			record.setOrderId(orderId);
			record.setReceiveTime(calendar.getTime());

			orderShardingWapper.orderMapper.updateByPrimaryKeySelective(record);

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					applyvalidationCall.getString("code"), "验证返回码");
			Logger.verifyEquals(false, applyvalidationCall.getData()
					.getBoolean("isValidationPass"), "验证isValidationPass");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_017() {
		Logger.start(true, "商品申请退款金额大于(商品可退金额+订单总运费)");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			Long orderId = order.getOrderId();

			//支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			Logger.comment("前置数据准备结束");

			//第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setRefundNumber(orderInfoPo.getProductCount());

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setRefundAmount(orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(orderInfoPo.getProductCount()))
					.add(order.getOrderFreight()).add(BigDecimal.ONE));

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("201", applyvalidationCall.getString("code"),
					"验证返回码");

			Logger.verifyEquals(
					String.format("OrderId:%s, 申请退运费金额不能大于实际可退运费金额", orderId),
					applyvalidationCall.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_018() {
		Logger.start(true, "订单商品不是包邮,商品退款加退部分商品运费");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			//第一个订单下的第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			int productNum = orderInfoPo.getProductCount();

			BigDecimal productFreight = orderInfoPo.getFreight();

			BigDecimal refundProductFreight = productFreight
					.subtract(new BigDecimal(2));

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setRefundNumber(orderInfoPo.getProductCount());

			validateRefundReq.setRefundAmount(refundProductFreight
					.add(refoundAmout));

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(refundProductFreight, BigDecimal
					.valueOf((Double) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_019() {
		Logger.start(true, "订单商品不是包邮,商品退款及退超过商品的运费，但不超过订单总运费");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			Long orderId = order.getOrderId();

			//支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			//第一个订单下的第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			int productNum = orderInfoPo.getProductCount();

			BigDecimal productFreight = orderInfoPo.getFreight();

			BigDecimal refundProductFreight = productFreight
					.add(new BigDecimal(2));

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setRefundAmount(refundProductFreight
					.add(refoundAmout));

			validateRefundReq.setRefundNumber(orderInfoPo.getProductCount());

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(refundProductFreight, BigDecimal
					.valueOf((Double) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_020() {
		Logger.start(true, "退款商品数量大于实际数量");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			//第一个订单下的第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			BigDecimal productFreight = orderInfoPo.getFreight();

			BigDecimal refundProductFreight = productFreight
					.add(new BigDecimal(2));

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setRefundAmount(refundProductFreight);

			validateRefundReq
					.setRefundNumber(orderInfoPo.getProductCount() + 1);

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("201", applyvalidationCall.getString("code"),
					"验证返回码");

			Logger.verifyEquals(String.format(
					"OrderId:%s(refundNumber:%s), 申请退商品数量不能大于实际商品数量",
					validateRefundReq.getOrderId(),
					validateRefundReq.getRefundNumber()), applyvalidationCall
					.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_021() {
		Logger.start(true, "订单商品不是包邮,申请单个商品退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			//第一个订单下的第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			int productNum = orderInfoPo.getProductCount();

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			//单件商品可退金额

			BigDecimal oneProRefund = refoundAmout.divide(new BigDecimal(
					productNum), 2, BigDecimal.ROUND_DOWN);

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setRefundNumber(1);

			validateRefundReq.setRefundAmount(oneProRefund);

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(BigDecimal.ZERO, BigDecimal
					.valueOf((Integer) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_022() {
		Logger.start(true, "订单商品不是包邮,申请单个商品退款加部分商品运费");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			//第一个订单下的第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			int productNum = orderInfoPo.getProductCount();

			BigDecimal productFreight = orderInfoPo.getFreight();

			BigDecimal refundProductFreight = productFreight
					.subtract(new BigDecimal(2));

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			//单件商品可退金额

			BigDecimal oneProRefund = refoundAmout.divide(new BigDecimal(
					productNum), 2, BigDecimal.ROUND_DOWN);
			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setRefundNumber(1);

			validateRefundReq.setRefundAmount(oneProRefund
					.add(refundProductFreight));

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(refundProductFreight, BigDecimal
					.valueOf((Double) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_023() {
		Logger.start(true, "订单商品不是包邮,申请单个商品退款加上不超过订单总运费");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			Long orderId = order.getOrderId();

			//支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			//第一个订单下的第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			int productNum = orderInfoPo.getProductCount();

			BigDecimal productFreight = orderInfoPo.getFreight();

			BigDecimal refundProductFreight = productFreight
					.add(new BigDecimal(2));

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			//单件商品可退金额

			BigDecimal oneProRefund = refoundAmout.divide(new BigDecimal(
					productNum), 2, BigDecimal.ROUND_DOWN);

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setRefundNumber(1);

			validateRefundReq.setRefundAmount(oneProRefund
					.add(refundProductFreight));

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(refundProductFreight, BigDecimal
					.valueOf((Double) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_024() {
		Logger.start(true, "订单商品不是包邮,申请单个商品退款加订单总运费");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			BigDecimal orderFreight = order.getOrderFreight();

			Long orderId = order.getOrderId();

			//支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			//第一个订单下的第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			int productNum = orderInfoPo.getProductCount();

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			//单件商品可退金额
			BigDecimal oneProRefund = refoundAmout.divide(new BigDecimal(
					productNum), 2, BigDecimal.ROUND_DOWN);

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setRefundNumber(1);

			validateRefundReq.setRefundAmount(oneProRefund.add(orderFreight));

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(orderFreight, BigDecimal
					.valueOf((Double) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_025() {
		Logger.start(true, "订单商品不是包邮,申请单个商品退款加上超过订单总运费");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			BigDecimal orderFreight = order.getOrderFreight();

			Long orderId = order.getOrderId();

			//支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			//第一个订单下的第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			int productNum = orderInfoPo.getProductCount();

			BigDecimal refundProductFreight = orderFreight
					.add(new BigDecimal(2));

			// 计算订单总价格 商品价格 X 数量+运费
			BigDecimal refoundAmout = orderInfoPo.getProductPrice()
					.multiply(new BigDecimal(productNum))
					.subtract(orderInfoPo.getYmtCouponAmount())
					.subtract(orderInfoPo.getSellerCouponAmount());

			//单件商品可退金额
			BigDecimal oneProRefund = refoundAmout.divide(new BigDecimal(
					productNum), 2, BigDecimal.ROUND_DOWN);

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setRefundNumber(1);

			validateRefundReq.setRefundAmount(oneProRefund
					.add(refundProductFreight));

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("201", applyvalidationCall.getString("code"),
					"验证返回码");
			Logger.verifyEquals(
					String.format("OrderId:%s, 申请退运费金额不能大于实际可退运费金额", orderId),
					applyvalidationCall.getString("msg"), "验证msg");

			DataUtil.verifyDecimalEquals(BigDecimal.ZERO, BigDecimal
					.valueOf((Integer) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_026() {
		Logger.start(true, "订单锁定后申请效验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			//锁定订单
			TradingCallService.markOrderLock(orderId, 1);

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);
			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals(ECode.FAILED201,
					applyvalidationCall.getString("code"), "验证返回码");

			Logger.verifyEquals("该订单已被客服锁定，暂时无法对该订单进行后续操作",
					applyvalidationCall.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_027() {
		Logger.start(true, "虚拟商品不能发起退货退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = BargainGroupService
					.placeVirtualProduct();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			//第一个订单下的第一个商品
			OrderDetailPo orderInfoPo = order.getOrderDetailPoList().get(0);

			BigDecimal refoundAmout = BigDecimal.ONE;

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderInfoPo.getCatalogId());

			validateRefundReq.setRefundNumber(1);

			validateRefundReq.setRefundAmount(refoundAmout);

			applyvalidationCall.setData(validateRefundReq);

			applyvalidationCall.callService();

			Logger.verifyEquals("201", applyvalidationCall.getString("code"),
					"验证返回码");

			Logger.verifyEquals("102", applyvalidationCall.getString("bcode"),
					"验证bcode");
			Logger.verifyEquals("虚拟商品不能发起退货退款",
					applyvalidationCall.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_028() {
		Logger.start(true, "买家承担鉴定费和鉴定物流费, 部分退货退款，申请退款金额小于商品可退鉴别费金额，校验失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(AuthenticateFreightTypeEnum.BUYER,
							BigDecimal.ONE, AuthenticateFeeTypeEnum.BUYER,
							BigDecimal.TEN);
			
			Order order = placeOrder.getOrderList().get(0);
			
			long orderId = order.getOrderId();
			Long sellerId = order.getSellerId();
			Long userId = placeOrder.getUserId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			Logger.comment("前置数据准备结束");
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrder.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(BigDecimal.ONE);
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();
			
			Logger.verifyEquals("201", applyvalidationCall.getString("code"),
					"验证返回码");
			Logger.verifyEquals(
					String.format("申请退款金额不能小于可退鉴别费金额", orderId),
					applyvalidationCall.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_029() {
		Logger.start(true, "买家承担鉴定费和鉴定物流费, 部分退货退款，申请退款金额大于商品可退鉴别费金额+商品金额，效验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(AuthenticateFreightTypeEnum.BUYER,
							BigDecimal.ONE, AuthenticateFeeTypeEnum.BUYER,
							BigDecimal.TEN);
			
			Order order = placeOrder.getOrderList().get(0);
			
			long orderId = order.getOrderId();
			Long sellerId = order.getSellerId();
			Long userId = placeOrder.getUserId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			Logger.comment("前置数据准备结束");
			
			BigDecimal userAuthenticateFee = order.getUserAuthenticateFee(1);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);
			
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setRefundAmount(userAuthenticateFee
					.add(BigDecimal.ONE));
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();
			
			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(BigDecimal.ZERO, BigDecimal
					.valueOf((Integer) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_030() {
		Logger.start(true, "买家承担鉴定费和鉴定物流费, 部分退货退款，申请退款金额大于商品可退鉴别费金额+商品金额，效验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(AuthenticateFreightTypeEnum.BUYER,
							BigDecimal.ONE, AuthenticateFeeTypeEnum.BUYER,
							BigDecimal.TEN);
			
			Order order = placeOrder.getOrderList().get(0);
			
			long orderId = order.getOrderId();
			Long sellerId = order.getSellerId();
			Long userId = placeOrder.getUserId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			Logger.comment("前置数据准备结束");
			
			BigDecimal userAuthenticateFee = order.getUserAuthenticateFee(1);
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);
			
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(placeOrder.getOrderList().get(0)
					.getOrderDetailPoList().get(0).getCatalogId());

			validateRefundReq.setRefundAmount(userAuthenticateFee.add(
					orderDetailPo.getProductPrice().multiply(BigDecimal.ONE))
					.add(BigDecimal.ONE));
			validateRefundReq.setRefundNumber(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();
			
			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(
					BigDecimal.ONE,
					BigDecimal.valueOf(applyvalidationCall.getData().getDouble(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_031() {
		Logger.start(true, "买家承担鉴定费和鉴定物流费, 全部退货退款，效验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);

			Order order = placeOrder.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long sellerId = order.getSellerId();
			Long userId = placeOrder.getUserId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);
			BigDecimal userAuthenticateFee = order.getUserAuthenticateFee(
					orderDetailPo.getProductCount()).add(
					order.getUserAuthenticateFreightFee());
			
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());

			validateRefundReq.setRefundAmount(userAuthenticateFee.add(
					orderDetailPo.getProductPrice().multiply(
							new BigDecimal(orderDetailPo.getProductCount())))
					.add(orderDetailPo.getFreight()));
			validateRefundReq.setRefundNumber(orderDetailPo.getProductCount());

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(orderDetailPo.getFreight(), BigDecimal
					.valueOf((Double) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_032() {
		Logger.start(true,
				"订单包含普通商品、买手鉴定费商品、买家鉴定费商品, 其他商品全部发起退款，全部退货退款申请，退款金额等于商品可退鉴别费金额+运费+商品金额，申请成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
			String sellername = EnvSetup.getData("autotest_sellerName1");

			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();

			Long userId = placeOrderReq.getUserId();
			
			// 第一个订单组
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellername);
			
			// 第一个订单
			OrderVo orderVo1 = TradingCallService.createOrderVo();
			
			orderVo1.withAuthenticateFreight(BigDecimal.ONE)
					.withAuthenticateFreightType(
							AuthenticateFreightTypeEnum.SELLER.getCode());
			
			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellername, 100);
			
			// 第一个商品
			OrderItemVo orderItemVo_order1_1 = TradingCallService.createOrderItemVo(
					product.getProductId(), product.getCatalogId());
			
			String normalCatalogId = orderItemVo_order1_1.getCatalogId();						

			// 第二个商品，买家鉴别费
			TradingProductVo pro = TradingProductVo.createPro(
					sellerId, sellername, 100);

			OrderItemVo orderItemVo_order1_2 = TradingCallService
					.createOrderItemVo(pro.getProductId(), pro.getCatalogId());
			String buyerAuthenticateCatalogId = orderItemVo_order1_2.getCatalogId();

			orderItemVo_order1_2
					.withAuthenticateFlag(true)
					.withAuthenticateFeeType(
							AuthenticateFeeTypeEnum.BUYER.getCode())
					.withAuthenticateFee(BigDecimal.TEN);
			
			// 第三个商品，买手鉴别费
			pro = TradingProductVo.createPro(sellerId, sellername, 100);

			OrderItemVo orderItemVo_order1_3 = TradingCallService
					.createOrderItemVo(pro.getProductId(), pro.getCatalogId());
			String sellerAuthenticateCatalogId = orderItemVo_order1_2.getCatalogId();
			
			orderItemVo_order1_3
					.withAuthenticateFlag(true)
					.withAuthenticateFeeType(
							AuthenticateFeeTypeEnum.SELLER.getCode())
					.withAuthenticateFee(BigDecimal.TEN);
			
			orderVo1.setOrderItems(Arrays.asList(orderItemVo_order1_1, orderItemVo_order1_2, orderItemVo_order1_3));
			orderGroupVo.setOrders(Arrays.asList(orderVo1));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));		
			
			TradingServiceResp placeOrder = TradingCallService
					.callPlaceOrderService(placeOrderReq);

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			Order order = placeOrder.getOrderList().get(0);
			Long orderId = order.getOrderId();
			
			// 找到普通商品
			OrderDetailPo normalOrderDetailPo = order
					.getOrderDetailPoList()
					.stream()
					.filter(orderDetailPo -> orderDetailPo.getCatalogId()
							.equalsIgnoreCase(normalCatalogId))
					.collect(Collectors.toList()).get(0);

			// 退第一个普通商品
			BigDecimal refundedFreight = BigDecimal.ONE;
			RefundService.buildSingleApplyRefundReq(orderId,
					normalOrderDetailPo, refundedFreight);
			
			// 找到买手鉴定费商品
			OrderDetailPo sellerAuthenticateOrderDetailPo = order
					.getOrderDetailPoList()
					.stream()
					.filter(orderDetailPo -> orderDetailPo.getCatalogId()
							.equalsIgnoreCase(sellerAuthenticateCatalogId))
					.collect(Collectors.toList()).get(0);

			// 退买手鉴定费商品
			RefundService.buildSingleApplyRefundReq(orderId,
					sellerAuthenticateOrderDetailPo, BigDecimal.ZERO);

			Logger.comment("前置数据准备结束");

			// 找到带有买家鉴定费商品
			OrderDetailPo buyerAuthenticateOrderDetailPo = order
					.getOrderDetailPoList()
					.stream()
					.filter(orderDetailPo -> orderDetailPo.getCatalogId()
							.equalsIgnoreCase(buyerAuthenticateCatalogId))
					.collect(Collectors.toList()).get(0);

			BigDecimal userAuthenticateFee = order.getUserAuthenticateFee(
					buyerAuthenticateOrderDetailPo.getProductCount()).add(
					order.getUserAuthenticateFreightFee());

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(buyerAuthenticateOrderDetailPo
					.getCatalogId());

			validateRefundReq.setRefundAmount(userAuthenticateFee.add(
					buyerAuthenticateOrderDetailPo.getProductPrice().multiply(
							new BigDecimal(buyerAuthenticateOrderDetailPo
									.getProductCount()))).add(
					buyerAuthenticateOrderDetailPo.getFreight()));
			validateRefundReq.setRefundNumber(buyerAuthenticateOrderDetailPo
					.getProductCount());

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(
					buyerAuthenticateOrderDetailPo.getFreight(),
					BigDecimal.valueOf(applyvalidationCall.getData().getDouble(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_033() {
		Logger.start(true,
				"订单包含普通商品、买家鉴定费商品, 其他商品全部发起退款，全部退货退款申请，退款金额等于商品可退鉴别费金额+运费+商品金额+鉴别运费，申请成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
			String sellername = EnvSetup.getData("autotest_sellerName1");

			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();

			Long userId = placeOrderReq.getUserId();

			// 第一个订单组
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(
					sellerId, sellername);

			// 第一个订单
			OrderVo orderVo1 = TradingCallService.createOrderVo();

			orderVo1.withAuthenticateFreight(BigDecimal.ONE)
					.withAuthenticateFreightType(
							AuthenticateFreightTypeEnum.SELLER.getCode());

			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellername, 100);

			// 第一个商品
			OrderItemVo orderItemVo_order1_1 = TradingCallService
					.createOrderItemVo(product.getProductId(),
							product.getCatalogId());

			String normalCatalogId = orderItemVo_order1_1.getCatalogId();

			// 第二个商品，买家鉴别费
			TradingProductVo pro = TradingProductVo.createPro(sellerId,
					sellername, 100);

			OrderItemVo orderItemVo_order1_2 = TradingCallService
					.createOrderItemVo(pro.getProductId(), pro.getCatalogId());
			String buyerAuthenticateCatalogId = orderItemVo_order1_2
					.getCatalogId();

			orderItemVo_order1_2
					.withAuthenticateFlag(true)
					.withAuthenticateFeeType(
							AuthenticateFeeTypeEnum.BUYER.getCode())
					.withAuthenticateFee(BigDecimal.TEN);

			orderVo1.setOrderItems(Arrays.asList(orderItemVo_order1_1,
					orderItemVo_order1_2));
			orderGroupVo.setOrders(Arrays.asList(orderVo1));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));
			
			TradingServiceResp placeOrder = TradingCallService
					.callPlaceOrderService(placeOrderReq);

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			Order order = placeOrder.getOrderList().get(0);
			Long orderId = order.getOrderId();
			
			// 找到普通商品
			OrderDetailPo normalOrderDetailPo = order
					.getOrderDetailPoList()
					.stream()
					.filter(orderDetailPo -> orderDetailPo.getCatalogId()
							.equalsIgnoreCase(normalCatalogId))
					.collect(Collectors.toList()).get(0);

			// 退第一个普通商品
			BigDecimal refundedFreight = BigDecimal.ONE;
			RefundService.buildSingleApplyRefundReq(orderId,
					normalOrderDetailPo, refundedFreight);

			Logger.comment("前置数据准备结束");

			// 找到带有买家鉴定费商品
			OrderDetailPo buyerAuthenticateOrderDetailPo = order
					.getOrderDetailPoList()
					.stream()
					.filter(orderDetailPo -> orderDetailPo.getCatalogId()
							.equalsIgnoreCase(buyerAuthenticateCatalogId))
					.collect(Collectors.toList()).get(0);

			BigDecimal userAuthenticateFee = order.getUserAuthenticateFee(
					buyerAuthenticateOrderDetailPo.getProductCount()).add(
					order.getUserAuthenticateFreightFee());

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(buyerAuthenticateOrderDetailPo
					.getCatalogId());

			validateRefundReq.setRefundAmount(userAuthenticateFee.add(
					buyerAuthenticateOrderDetailPo.getProductPrice().multiply(
							new BigDecimal(buyerAuthenticateOrderDetailPo
									.getProductCount()))).add(
					buyerAuthenticateOrderDetailPo.getFreight()));
			validateRefundReq.setRefundNumber(buyerAuthenticateOrderDetailPo
					.getProductCount());

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(
					buyerAuthenticateOrderDetailPo.getFreight(),
					BigDecimal.valueOf(applyvalidationCall.getData().getDouble(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_034() {
		Logger.start(true, "先退一件商品+部分运费，再退一件商品，申请退款数量>商品总数，校验失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingCallService.payOrder();
			
			Order order = placeOrder.getOrderList().get(0);
			long orderId = order.getOrderId();
			
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);
			
			// 先退一个商品
			BigDecimal refundedFreight = BigDecimal.ONE;
			Integer refundedProductCount = 1;
			
			RefundService.buildSingleApplyRefundReq(orderId, orderDetailPo,
					refundedFreight, refundedProductCount);
			
			Logger.comment("前置数据准备结束");

			BigDecimal userAuthenticateFee = order.getUserAuthenticateFee(
					orderDetailPo.getProductCount()).add(
					order.getUserAuthenticateFreightFee());
			
			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setRefundAmount(userAuthenticateFee.add(
					orderDetailPo.getProductPrice().multiply(
							new BigDecimal(orderDetailPo.getProductCount() - refundedProductCount)))
					.add(orderDetailPo.getFreight()).subtract(refundedFreight));
			validateRefundReq.setRefundNumber(orderDetailPo.getProductCount());

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("201", applyvalidationCall.getString("code"),
					"验证返回码");
			Logger.verifyEquals(
					String.format("申请退商品数量不能大于实际商品数量", orderId),
					applyvalidationCall.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_035() {
		Logger.start(true,
				"订单包含普通商品、买家鉴定费商品, 鉴定中心发货，其他商品全部发起退款，全部退货退款申请，退款金额等于商品可退鉴别费金额+运费+商品金额+鉴别运费，申请校验失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
			String sellername = EnvSetup.getData("autotest_sellerName1");

			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();

			Long userId = placeOrderReq.getUserId();

			// 第一个订单组
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(
					sellerId, sellername);

			// 第一个订单
			OrderVo orderVo1 = TradingCallService.createOrderVo();

			orderVo1.withAuthenticateFreight(BigDecimal.ONE)
					.withAuthenticateFreightType(
							AuthenticateFreightTypeEnum.SELLER.getCode());

			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellername, 100);

			// 第一个商品
			OrderItemVo orderItemVo_order1_1 = TradingCallService
					.createOrderItemVo(product.getProductId(),
							product.getCatalogId());

			String normalCatalogId = orderItemVo_order1_1.getCatalogId();

			// 第二个商品，买家鉴别费
			TradingProductVo pro = TradingProductVo.createPro(sellerId,
					sellername, 100);

			OrderItemVo orderItemVo_order1_2 = TradingCallService
					.createOrderItemVo(pro.getProductId(), pro.getCatalogId());
			String buyerAuthenticateCatalogId = orderItemVo_order1_2
					.getCatalogId();

			orderItemVo_order1_2
					.withAuthenticateFlag(true)
					.withAuthenticateFeeType(
							AuthenticateFeeTypeEnum.BUYER.getCode())
					.withAuthenticateFee(BigDecimal.TEN);

			orderVo1.setOrderItems(Arrays.asList(orderItemVo_order1_1,
					orderItemVo_order1_2));
			orderGroupVo.setOrders(Arrays.asList(orderVo1));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));
			
			TradingServiceResp placeOrder = TradingCallService
					.callPlaceOrderService(placeOrderReq);

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			Order order = placeOrder.getOrderList().get(0);
			Long orderId = order.getOrderId();
			
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			Thread.sleep(3000);
			
			// 国际段发货
			TradingCallService.delivery(sellerId, orderId, LogisticsTypeEnum.OVERSEAS_DELIVERY, "Y013", 1, true);
			Thread.sleep(3000);
			
			// 鉴定中心发货
			TradingCallService.authenticateDelivery(sellerId, orderId);
			Thread.sleep(3000);
			
			// 找到普通商品
			OrderDetailPo normalOrderDetailPo = order
					.getOrderDetailPoList()
					.stream()
					.filter(orderDetailPo -> orderDetailPo.getCatalogId()
							.equalsIgnoreCase(normalCatalogId))
					.collect(Collectors.toList()).get(0);

			// 退第一个普通商品
			BigDecimal refundedFreight = normalOrderDetailPo.getFreight();
			RefundService.buildSingleApplyRefundReq(orderId,
					normalOrderDetailPo, refundedFreight);

			Logger.comment("前置数据准备结束");

			// 找到带有买家鉴定费商品
			OrderDetailPo buyerAuthenticateOrderDetailPo = order
					.getOrderDetailPoList()
					.stream()
					.filter(orderDetailPo -> orderDetailPo.getCatalogId()
							.equalsIgnoreCase(buyerAuthenticateCatalogId))
					.collect(Collectors.toList()).get(0);

			BigDecimal userAuthenticateFee = order.getUserAuthenticateFee(
					buyerAuthenticateOrderDetailPo.getProductCount()).add(
					order.getUserAuthenticateFreightFee());

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(buyerAuthenticateOrderDetailPo
					.getCatalogId());

			validateRefundReq.setRefundAmount(userAuthenticateFee.add(
					buyerAuthenticateOrderDetailPo.getProductPrice().multiply(
							new BigDecimal(buyerAuthenticateOrderDetailPo
									.getProductCount()))).add(
					buyerAuthenticateOrderDetailPo.getFreight()));
			validateRefundReq.setRefundNumber(buyerAuthenticateOrderDetailPo
					.getProductCount());

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("201", applyvalidationCall.getString("code"),
					"验证返回码");
			Logger.verifyEquals(
					String.format("OrderId:%s, 申请退运费金额不能大于实际可退运费金额", orderId),
					applyvalidationCall.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_036() {
		Logger.start(true,
				"订单包含普通商品、买家鉴定费商品, 鉴定中心发货，其他商品全部发起退款，全部退货退款申请，退款金额等于商品可退鉴别费金额+运费+商品金额，申请校验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
			String sellername = EnvSetup.getData("autotest_sellerName1");

			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();

			Long userId = placeOrderReq.getUserId();

			// 第一个订单组
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(
					sellerId, sellername);

			// 第一个订单
			OrderVo orderVo1 = TradingCallService.createOrderVo();

			orderVo1.withAuthenticateFreight(BigDecimal.ONE)
					.withAuthenticateFreightType(
							AuthenticateFreightTypeEnum.SELLER.getCode());

			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellername, 100);

			// 第一个商品
			OrderItemVo orderItemVo_order1_1 = TradingCallService
					.createOrderItemVo(product.getProductId(),
							product.getCatalogId());

			String normalCatalogId = orderItemVo_order1_1.getCatalogId();

			// 第二个商品，买家鉴别费
			TradingProductVo pro = TradingProductVo.createPro(sellerId,
					sellername, 100);

			OrderItemVo orderItemVo_order1_2 = TradingCallService
					.createOrderItemVo(pro.getProductId(), pro.getCatalogId());
			String buyerAuthenticateCatalogId = orderItemVo_order1_2
					.getCatalogId();

			orderItemVo_order1_2
					.withAuthenticateFlag(true)
					.withAuthenticateFeeType(
							AuthenticateFeeTypeEnum.BUYER.getCode())
					.withAuthenticateFee(BigDecimal.TEN);

			orderVo1.setOrderItems(Arrays.asList(orderItemVo_order1_1,
					orderItemVo_order1_2));
			orderGroupVo.setOrders(Arrays.asList(orderVo1));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));
			
			TradingServiceResp placeOrder = TradingCallService
					.callPlaceOrderService(placeOrderReq);

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			Order order = placeOrder.getOrderList().get(0);
			Long orderId = order.getOrderId();
			
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			Thread.sleep(3000);
			
			// 国际段发货
			TradingCallService.delivery(sellerId, orderId, LogisticsTypeEnum.OVERSEAS_DELIVERY, "Y013", 1, true);
			Thread.sleep(3000);
			
			// 鉴定中心发货
			TradingCallService.authenticateDelivery(sellerId, orderId);
			Thread.sleep(5000);
			
			// 找到普通商品
			OrderDetailPo normalOrderDetailPo = order
					.getOrderDetailPoList()
					.stream()
					.filter(orderDetailPo -> orderDetailPo.getCatalogId()
							.equalsIgnoreCase(normalCatalogId))
					.collect(Collectors.toList()).get(0);

			// 退第一个普通商品
			BigDecimal refundedFreight = normalOrderDetailPo.getFreight();
			RefundService.buildSingleApplyRefundReq(orderId,
					normalOrderDetailPo, refundedFreight);

			Logger.comment("前置数据准备结束");

			// 找到带有买家鉴定费商品
			OrderDetailPo buyerAuthenticateOrderDetailPo = order
					.getOrderDetailPoList()
					.stream()
					.filter(orderDetailPo -> orderDetailPo.getCatalogId()
							.equalsIgnoreCase(buyerAuthenticateCatalogId))
					.collect(Collectors.toList()).get(0);

			BigDecimal userAuthenticateFee = order.getUserAuthenticateFee(
					buyerAuthenticateOrderDetailPo.getProductCount());

			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(buyerAuthenticateOrderDetailPo
					.getCatalogId());

			validateRefundReq.setRefundAmount(userAuthenticateFee.add(
					buyerAuthenticateOrderDetailPo.getProductPrice().multiply(
							new BigDecimal(buyerAuthenticateOrderDetailPo
									.getProductCount()))).add(
					buyerAuthenticateOrderDetailPo.getFreight()));
			validateRefundReq.setRefundNumber(buyerAuthenticateOrderDetailPo
					.getProductCount());

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(
					buyerAuthenticateOrderDetailPo.getFreight(),
					BigDecimal.valueOf(applyvalidationCall.getData().getDouble(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ValidateRefund_037() {
		Logger.start(true, "一个买家鉴定费商品，一个普通商品， 接单后，买家鉴定费商品全部退货退款，退款金额>商品可退鉴别费金额+鉴别运费，校验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticateWithTwoTypeProduct(AuthenticateFreightTypeEnum.BUYER,
							BigDecimal.ONE, AuthenticateFeeTypeEnum.BUYER,
							BigDecimal.TEN);
			
			Order order = placeOrder.getOrderList().get(0);
			
			long orderId = order.getOrderId();
			Long sellerId = order.getSellerId();
			Long userId = placeOrder.getUserId();

			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					placeOrder.getMainOrderId());

			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			Logger.comment("前置数据准备结束");
			
			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().stream()
					.filter(od -> od.getAuthenticateFlag() == true)
					.collect(Collectors.toList()).get(0);
			BigDecimal userAuthenticateFee = order.getUserAuthenticateFee(orderDetailPo.getProductCount());
			
			validateRefundReq.setOrderId(orderId);

			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setRefundAmount(orderDetailPo.getProductPrice()
					.multiply(new BigDecimal(orderDetailPo.getProductCount()))
					.add(orderDetailPo.getFreight()).add(userAuthenticateFee)
					.add(order.getUserAuthenticateFreightFee()));
			validateRefundReq.setRefundNumber(orderDetailPo.getProductCount());

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();
			
			Logger.verifyEquals("200", applyvalidationCall.getString("code"),
					"验证返回码");

			DataUtil.verifyDecimalEquals(orderDetailPo.getFreight(), BigDecimal
					.valueOf((Double) applyvalidationCall.getData().get(
							"refundFreight")), "验证refundFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
